package com.xuexi.leetcode;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @Author: 梁二东
 * @Date: 2024/11/27 14:18
 * @Description: 有效的括号 eg： () , ([]){}[]{}()
 * 无效的括号 eg： ){}(, (})
 **/
public class ValidBracket {

    public static void main(String[] args) {
//        Deque<Character> deque = new LinkedList<>();
//        String s = "abcde";
//        for (int i = 0; i < s.length(); i++) {
//            deque.push(s.charAt(i));
//        }
//        //edcba
//        System.out.println(deque);

        ValidBracket validBracket = new ValidBracket();
        boolean b = validBracket.validBracket("([]){}[]{}()");
//        boolean b = validBracket.validBracket("){");
//        boolean b = validBracket.validBracket("))");
        System.out.println(b);
    }

    public boolean validBracket(String s) {
        if (s.length() % 2 == 1) {
            return false;
        }
        Deque<Character> deque = new LinkedList<>();
        Map<Character, Character> map = new HashMap<Character, Character>() {{
            put('(', ')');
            put('{', '}');
            put('[', ']');
        }};
        // ([]){}[]{}()
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (map.containsKey(ch)) {
                deque.push(map.get(ch));
            } else {
                Character pop = deque.pollFirst();
                if (pop == null || ch != pop) {
                    return false;
                }
            }
        }
        return deque.isEmpty();
    }
    /*public boolean isValid(String s) {
        int n = s.length();
        if (n % 2 == 1) {
            return false;
        }

        Map<Character, Character> pairs = new HashMap<Character, Character>() {{
            put(')', '(');
            put(']', '[');
            put('}', '{');
        }};
        Deque<Character> stack = new LinkedList<Character>();
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if (pairs.containsKey(ch)) {
                if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
                    return false;
                }
                stack.pop();
            } else {
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }
*/
}
